/*
 * jquery.juice.inputlist.js
 *
 * Juice Library Input List v0.1.0
 * Date: 2009-04-30
 * Requires: jQuery v1.3 or later
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Copyright 2009 Steve Whiteley (http://jui.ce.it)
 */

(function($) {
	$.fn.inputList = function(callerSettings) {
		var settings = $.extend(true, {}, $.fn.inputList.settings, callerSettings);
		return this.each(function() {
			var n = $(this), l, b;
			var setup = function() {
				if (settings.errors) {
					$(settings.errors).hide();
				}
				l = (settings.list ? $(settings.list) : n.parent());
				b = $('<button />')
					.text('Add')
					.addClass('mini')
					.insertAfter(n)
					.click(function(e) {
						e.preventDefault();
						add(n.val());
					});
				$('input[name="' + n.attr('name') + '"]', l)
					.each(function() {
						$(this).not(n).remove();
						add($(this).val());
						error(null);					
					});
				if ($.isArray(settings.events)) {
					n.bind(settings.events.join(' '), function(e) {
						e.preventDefault();
						b.trigger('click');
					});
					if ($.inArray('enter', settings.events) != -1) {
						n.keypress(function(e) {
							if (e.keyCode == 13) {
								e.preventDefault();
								n.trigger('enter');
							}
						});
					}
				}
			};
			var add = function(value) {
				error(null);
				if ($.trim(value) != '') {
					var name = settings.name || n.attr('name');
					var dupes = $('input[name="' + name + '"]', l)
						.not(n)
						.filter(function(index) {
							return $(this).val() == value;
						}).length;
					if (dupes < 1) {
						if ($.isFunction(settings.validate)) {
							var validator = settings.validate(value);
							value = validator.value;
							if (validator.error) {
								error(validator.error);
							}
						}
						if (value) {
							var input = $('<input />')
								.attr({
									type: 'hidden',
									name: name
								})
								.val(value);
							var item = $('<a />')
								.attr('href', '#')
								.html(value)
								.addClass(settings.itemClass)
								.hide()
								.append(input)
								.appendTo(l)
								.fadeIn()
								.click(function(e) {
									e.preventDefault();
									$(this).fadeOut(function() {
										$(this).remove();
									});
								});
							n.val('');
						}
					} else {
						error(settings.messages.duplicate);
					}
				} else {
					error(settings.messages.empty);
				}
				n.focus();
			};
			var error = function(err) {
				var msg;
				if (settings.errors && (msg = $(settings.errors))) {
					if (err) {
						msg.html(err).show();
					} else {
						msg.html('').hide();
					}
				}
			};
			setup();
		});
	};
	$.fn.inputList.settings = {
		errors: false,
		events: ['enter'],
		itemClass: false,
		list: false,
		messages: {
			duplicate: 'Duplicate item',
			empty: 'Empty item name'
		},
		name: false,
		validate: false
	};
})(jQuery);
